﻿package net.wangit.view.unit.grid;

import net.wangit.Element;
import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.util.Checker;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.component.CheckBoxFieldElement;
import net.wangit.view.element.component.GlobalRadioFieldElement;
import net.wangit.view.element.component.LabelElement;
import net.wangit.view.unit.UnitComponent;
import net.wangit.view.unit.combine.CombineProcessor;
import net.wangit.view.unit.sum.SumProcessor;

public final class ListGrid extends Grid {

	private static final String CLICK_MARK = " onmousedown=\"$fRC(event)\"";
	private static final String MOVE_MARK = " onmouseover=\"$fRO(event)\" onmouseout=\"$fRX(event)\"";
	private static MapList radioSelect;

	public ListGrid(ActionContext ac, UnitComponent uc) {
		super(ac, uc);
		if (uc.size() > 0) {
			Element select = addSelectColumn();
			Element index = addIndexColumn();
			CombineProcessor.combine(ac, uc);
			SumProcessor.sum(ac, uc, select, index);
		}
	}

	public String write(ActionContext ac, MapList data) {
		if (uc.size() == 0)
			return "";
		StringBuffer html = new StringBuffer();
		html.append("<tr><td>");
		beginTable(html);
		if (uc.isShowListHead()) {
			String headCss = uc.isShowCellBorder() ? "HB" : "H";
			html.append("<thead>");
			top.writeListHead(ac, html, headCss);
			html.append("</thead>");
		}
		html.append("<tbody>");
		if (!Checker.isEmpty(data)) {
			for (int i = 0; i < data.size(); i++) {
				Row row = data.getRow(i);
				if ("3".equals(row.get("")))
					top.setVisible(false);
				else
					top.setVisible(true);
				top.setRowId(Integer.toString(i));
				top.write(ac, html, row, i, uc.getRowCss(i));
			}

		}
		html.append("</tbody>");
		html.append("</table>");
		html.append("</td></tr>");
		if (uc.isAppendInsertScript())
			writeInsertContent(ac, html);
		return html.toString();
	}

	protected void beginTable(StringBuffer html) {
		html.append("<table cellpadding=\"0\" width=\"100%\" id=\"").append(
				"u_g_").append(uc.getId()).append("\"");
		if (uc.isClickMark())
			html.append(" onmousedown=\"$fRC(event)\"");
		if (uc.isMoveMark())
			html
					.append(" onmouseover=\"$fRO(event)\" onmouseout=\"$fRX(event)\"");
		if (uc.isShowCellBorder())
			html.append(" cellspacing=\"1\" class=\"").append("DB")
					.append("\"");
		else
			html.append(" cellspacing=\"0\"");
		html.append(">");
		setColsGroup(html);
	}

	private void writeInsertContent(ActionContext ac, StringBuffer html) {
		html.append("<tr style=\"display:none\"><td>");
		html.append("<textarea id=\"").append("_in_").append("\">");
		top.setRowId("temp");
		top.write(ac, html, insertRow, -1, "RI");
		html.append("</textarea>");
		html.append("</td></tr>");
	}

	private Element addSelectColumn() {
		int selectMode = uc.getListSelectMode();
		if (selectMode == 0)
			return null;
		String id = (new StringBuilder("_s_")).append(uc.getId()).toString();
		ElementComponent select;
		if (selectMode == 1) {
			select = new GlobalRadioFieldElement(id, radioSelect);
			select.setCss("SR");
		} else {
			select = new CheckBoxFieldElement(id, "");
			StringBuffer name = (new StringBuffer("<input id=\"")).append(
					"_sa_").append(
					"\" type=\"checkbox\" onclick=\"$fSelectAll('").append(
					uc.getId()).append("',this)\" class=\"").append("SC")
					.append("\"/>");
			select.setName(name.toString());
			select.setCustom((new StringBuilder("onclick=\"$fResetCheck('"))
					.append(uc.getId()).append("')\"").toString());
			MapList data = uc.getData();
			if (!Checker.isEmpty(data)) {
				for (int i = 0; i < data.size(); i++)
					select.setName(i, "");

			}
		}
		select.setTdAlign(2);
		select.setTdWidth(20);
		select.setTdWidthMode(1);
		select.setShowMode(3);
		addColumn(select, new String[] { "C", "20px", "1" });
		return select;
	}

	private Element addIndexColumn() {
		if (!uc.isShowListIndex())
			return null;
		LabelElement index = new LabelElement();
		index.setTdCustom("id=\"_i_\"");
		MapList data = uc.getData();
		if (!Checker.isEmpty(data)) {
			for (int i = 0; i < data.size(); i++)
				index.setName(i, Integer.toString(i + 1));

		}
		addColumn(index, new String[] { "C", "20px", "1" });
		return index;
	}

	private void addColumn(ElementComponent element, String colGroup[]) {
		if (top == null)
			return;
		element.setTdRowSpan(top.getRowSpan());
		top.addColumn(element);
		if (colsConfig == null) {
			colsConfig = new String[1][];
		} else {
			String newColConfig[][] = new String[colsConfig.length + 1][];
			for (int i = 0; i < colsConfig.length; i++)
				newColConfig[i + 1] = colsConfig[i];

			colsConfig = newColConfig;
		}
		colsConfig[0] = colGroup;
	}

	static {
		radioSelect = new MapList();
		radioSelect.put(0, "value", "1");
		radioSelect.put(0, "name", "");
	}
}
